package com.example.zsfinacialmanage.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;

import com.example.zsfinacialmanage.R;
import com.example.zsfinacialmanage.adapter.OutpayAdapter;
import com.example.zsfinacialmanage.bean.IncomeBean;
import com.example.zsfinacialmanage.bean.OutpayBean;
import com.example.zsfinacialmanage.db.MyDBHelper;

import java.util.ArrayList;
import java.util.List;

public class PayDetailActivity extends AppCompatActivity {
    //1.定义对象
    RecyclerView recy_view;
    MyDBHelper mhelper;
    SQLiteDatabase db;
    List<OutpayBean> arr2 = new ArrayList<OutpayBean>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_detail);
        //绑定控件
        initView();
        //准备数据
        initData();

        //设计每一行的子布局
        //创建适配器
        OutpayAdapter adapter = new OutpayAdapter(PayDetailActivity.this, arr2);
        //将适配器和布局管理器加载到控件当中
        StaggeredGridLayoutManager st = new StaggeredGridLayoutManager(StaggeredGridLayoutManager.VERTICAL, 1);
        recy_view.setLayoutManager(st);
        recy_view.setAdapter(adapter);
    }
    //2.绑定控件
    private void initView() {
        recy_view = findViewById(R.id.recy_view_outdetail);
        mhelper = new MyDBHelper(PayDetailActivity.this);
        db = mhelper.getWritableDatabase();
    }
    //3.准备数据
    private void initData() {
        //从数据库查询所有的新增收入信息，取出数据
        Cursor cursor = db.rawQuery("SELECT * FROM out_pay", null);

        if (cursor != null && cursor.moveToFirst()) {
            do {
                int idColumnIndex = cursor.getColumnIndex("id");
                if (idColumnIndex == -1) {
                    throw new IllegalArgumentException("Column 'id' not found in the table.");
                }
                int myid = cursor.getInt(idColumnIndex);

                int outmoneyColumnIndex = cursor.getColumnIndex("outmoney");
                if (outmoneyColumnIndex == -1) {
                    throw new IllegalArgumentException("Column 'outmoney' not found in the table.");
                }
                double mymoney = cursor.getDouble(outmoneyColumnIndex);

                int outtimeColumnIndex = cursor.getColumnIndex("outtime");
                if (outtimeColumnIndex == -1) {
                    throw new IllegalArgumentException("Column 'outtime' not found in the table.");
                }
                String mytime = cursor.getString(outtimeColumnIndex);

                int outtypeColumnIndex = cursor.getColumnIndex("outtype");
                if (outtypeColumnIndex == -1) {
                    throw new IllegalArgumentException("Column 'outtype' not found in the table.");
                }
                String mytype = cursor.getString(outtypeColumnIndex);

                int outpayerColumnIndex = cursor.getColumnIndex("outpayer");
                if (outpayerColumnIndex == -1) {
                    throw new IllegalArgumentException("Column 'outpayer' not found in the table.");
                }
                String mypayer = cursor.getString(outpayerColumnIndex);

                int outremarkColumnIndex = cursor.getColumnIndex("outremark");
                if (outremarkColumnIndex == -1) {
                    throw new IllegalArgumentException("Column 'outremark' not found in the table.");
                }
                String myremark = cursor.getString(outremarkColumnIndex);

                OutpayBean outpayBean = new OutpayBean(myid, mymoney, mytime, mytype, mypayer, myremark);
                arr2.add(outpayBean);
            } while (cursor.moveToNext());
        }

        // 关闭游标
        if (cursor != null) {
            cursor.close();
        }
    }
}
